package com.night.icm.controller.base;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.SimpleDataDTO;
import com.night.icm.model.base.TeacherDTO;
import com.night.icm.service.base.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * TeacherController
 *
 * @author Liao ZhiYong
 * @date 2019/12/21
 */
@Api(tags = {"教师管理"})
@RestController
@RequestMapping("/api/icm-teacher")
public class TeacherController {

    private final TeacherService teacherService;

    @Autowired
    public TeacherController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    @ApiOperation(value = "分页查询教师")
    @GetMapping(value = "")
    public PageDataDTO<TeacherDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "所属学院") @RequestParam(value = "academyId", required = false) String academyId,
            @ApiParam(value = "教师编号") @RequestParam(value = "code", required = false) String code,
            @ApiParam(value = "教师姓名") @RequestParam(value = "name", required = false) String name
    ) {
        return this.teacherService.findOnePage(page, size, sort, academyId, code, name);
    }

    @ApiOperation(value = "添加教师")
    @PostMapping(value = "")
    public TeacherDTO create(
            @ApiParam(value = "教师信息", required = true) @RequestBody @Validated TeacherDTO teacherDTO
    ) {
        return this.teacherService.create(teacherDTO);
    }

    @ApiOperation(value = "删除教师")
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "id", required = true) @PathVariable(name = "id") String id) {
        this.teacherService.delete(id);
    }

    @ApiOperation(value = "更新教师信息")
    @PutMapping(value = "/{id}")
    public TeacherDTO update(
            @ApiParam(value = "教师id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "教师信息", required = true) @RequestBody @Validated TeacherDTO teacherDTO
    ) {
        return this.teacherService.update(id, teacherDTO);
    }

    @ApiOperation(value = "通过id查找教师")
    @GetMapping(value = "/{id}")
    public TeacherDTO findById(@ApiParam(value = "教师id", required = true) @PathVariable(name = "id") String id) {
        return this.teacherService.findById(id);
    }

    @ApiOperation(value = "查找所有教师，可选择排序")
    @GetMapping(value = "/all")
    public List<TeacherDTO> findAll(
            @ApiParam(value = "排序参数") @RequestParam(value = "param", required = false, defaultValue = "id") String param,
            @ApiParam(value = "排序模式: asc/desc") @RequestParam(value = "pattern", required = false, defaultValue = "asc") String pattern
    ) {
        return this.teacherService.findAll(param, pattern);
    }

    @ApiOperation(value = "检查教师是否已存在")
    @GetMapping(value = "/checkCodeExist")
    public SimpleDataDTO<Boolean> checkCodeExist(@ApiParam(value = "教师code", required = true) @RequestParam(value = "code") String code) {
        return this.teacherService.checkCodeExist(code);
    }

    @ApiOperation(value = "根据用户id查找教师")
    @GetMapping(value = "/byUser/{userId}")
    public TeacherDTO findByUserId(@ApiParam(value = "用户id", required = true) @PathVariable(name = "userId") String userId) {
        return this.teacherService.findByUserId(userId);
    }
}